﻿using brand.cyt369.com.models;
using brand.cyt369.com.models.Base;
using brand.cyt369.com.MODELS;
using Humanizer.Localisation.TimeToClockNotation;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace brand.cyt369.com.Controllers
{
    public class SearchController : Controller
    {
        /// <summary>
        /// a标签链接入口
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Index(string id)
        {
            string base64Str=id;
            int total=0;
            //int page = Request.Query.GetInteger("page", 1);
            //int rows = brand.cyt369.com.utility.SystemSettingService.Instance.PageSize;
            //int? categoryID = Request.Query.GetInteger("categoryID", 0);
            //int parentCategoryID = Request.Query.GetInteger("parentCategoryID", 0);
            //int? amount = Request.Query.GetInteger("amount", 0);
            //int? cityID = Request.Query.GetInteger("cityID", 0);
            //string searchStr = Request.Query.GetString("searchStr");

            //页数，行数，当前页面的父类别ID
            int page=1, rows, parentCategoryID=0;
            //当前选择的类别ID,选择的金额，选择的城市
            int? categoryID = 0, amount = 0, cityID = 0;
            rows = brand.cyt369.com.utility.SystemSettingService.Instance.PageSize;
            string searchStr=null;
            if (!string.IsNullOrEmpty(base64Str))
            {
                var argsJson = Base64.Base64ToJSON(base64Str);
                page = argsJson.GetInteger("page",1);
                categoryID = argsJson.GetInteger("categoryID", 0);
                parentCategoryID = argsJson.GetInteger("parentCategoryID", 0);
                amount = argsJson.GetInteger("amount", 0);
                cityID = argsJson.GetInteger("cityID", 0);
                searchStr = argsJson.GetString("searchStr");
            }
            IQueryable<brand.cyt369.com.models.Brand> brands;
            return SearchView("Index", page, rows, parentCategoryID, categoryID, amount, cityID, searchStr);
        }

        /// <summary>
        /// JS调用入口
        /// </summary>
        /// <returns></returns>
        public IActionResult Index2()
        {
            int page = Request.Query.GetInteger("page", 1);
            int rows = brand.cyt369.com.utility.SystemSettingService.Instance.PageSize;
            int? categoryID = Request.Query.GetInteger("categoryID", 0);
            int parentCategoryID = Request.Query.GetInteger("parentCategoryID", 0);
            int? amount = Request.Query.GetInteger("amount", 0);
            int? cityID = Request.Query.GetInteger("cityID", 0);
            string searchStr = Request.Query.GetString("searchStr");
            return SearchView("Index",page,rows,parentCategoryID,categoryID,amount,cityID,searchStr);
        }

        public IActionResult SearchView(string viewName,int page,int rows, int parentCategoryID , int? categoryID,int? amount,int? cityID,string searchStr)
        {
            int total = 0;
            IQueryable<brand.cyt369.com.models.Brand> brands;
            IQueryable<brand.cyt369.com.models.Brand> brands2;
            //初始化类别
            if (categoryID.HasValue && categoryID != 0)//如果选择的类别存在
            {
                brand.cyt369.com.models.Category category = brand.cyt369.com.dal.CategoryService.Instance.CategorysGetByID(categoryID);
                var parentCategory = brand.cyt369.com.dal.CategoryService.Instance.CategorysGetByParentID_InclueChilds(category.ParentID);
                ViewBag.category = parentCategory;
                //获取查询结果
                brands = brand.cyt369.com.dal.BrandService.Instance.GetBrands(page: page, rows: rows, ref total, searchStr: searchStr, categoryID: categoryID, amount: amount, cityID: cityID);
                brands2 = brand.cyt369.com.dal.BrandService.Instance.GetBrands_NoIncludeImg(searchStr: searchStr, categoryID: categoryID, amount: amount, cityID: cityID);
            }
            else if (parentCategoryID != 0) //如果选择的 '不限'
            {
                var parentCategory = brand.cyt369.com.dal.CategoryService.Instance.CategorysGetByParentID_InclueChilds(parentCategoryID);
                ViewBag.category = parentCategory;
                //获取查询结果
                brands = brand.cyt369.com.dal.BrandService.Instance.GetBrands(page: page, rows: rows, ref total, searchStr: searchStr, categoryID: parentCategoryID, amount: amount, cityID: cityID);
                brands2= brand.cyt369.com.dal.BrandService.Instance.GetBrands_NoIncludeImg(searchStr: searchStr, categoryID: parentCategoryID, amount: amount, cityID: cityID);
            }
            else
            {
                var allParentCategorys = brand.cyt369.com.dal.CategoryService.Instance.CategorysGetByParentID(0);//轮播左侧的类别
                ViewBag.allParentCategorys = allParentCategorys;
                //获取查询结果
                brands = brand.cyt369.com.dal.BrandService.Instance.GetBrands(page: page, rows: rows, ref total, searchStr: searchStr, categoryID: categoryID, amount: amount, cityID: cityID);
                brands2 = brand.cyt369.com.dal.BrandService.Instance.GetBrands_NoIncludeImg(searchStr: searchStr, categoryID: categoryID, amount: amount, cityID: cityID);
            }
            var citys = new List<brand.cyt369.com.models.Base.City>();
            //初始化城市
            if (brands2 != null)
            {
                var dict = new Dictionary<int?, int>();
                foreach (var b in brands2)
                {
                    //if(b.FranchiseRegionID!=null)
                    if (!dict.ContainsKey(b.FranchiseRegionID))
                    {
                        dict[b.FranchiseRegionID] = 0;
                        var city = brand.cyt369.com.dal.CityService.Instance.GetCityByID(b.FranchiseRegionID);
                        if (city != null) { citys.Add(city); }
                    }
                }
            }

            ViewBag.citys = citys;
            ViewBag.searchStr = searchStr;
            ViewBag.categoryID = categoryID;
            ViewBag.amount = amount;
            ViewBag.brands = brands;
            ViewBag.total = total;
            ViewBag.page = page;
            return View(viewName);
        }
    }
}
